﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Text;

namespace TextEditorDP
{
    public class WordPad : Form, Widget, Observer
    {
        #region	Local Variables

        static public WordPad wordpad;

        // UI Components
        GuiFactory guiFactory;
        public CPanel toolbarpanel;
        public CRichTextBox edit;
        public WordPadMenu menu;
        public CStatusBar status;

        // Document
        public string filename;
        public RichTextBoxStreamType filetype;

        // State
        public bool changed;
        bool IsHelp = false;

        //Formatting Bar Buttons
        public CToolStrip formattingBar;
        ToolStripButton cmdUnderline = new ToolStripButton();
        ToolStripButton cmdBold = new ToolStripButton();
        ToolStripButton cmdItalic = new ToolStripButton();
        ToolStripSeparator toolStripSeparator1 = new ToolStripSeparator();
        ToolStripDropDownButton lstColors = new ToolStripDropDownButton();
        ToolStripDropDownButton lstFonts = new ToolStripDropDownButton();
        ToolStripDropDownButton lstZoom = new ToolStripDropDownButton();
        ToolStripDropDownButton lstFontSize = new ToolStripDropDownButton();
        ToolStripSeparator toolStripSeparator2 = new ToolStripSeparator();
        Button cmdAddImage = new Button();

        // Action Bar Buttons
        public CToolStrip actionBar;
        ToolStripDropDownButton lstUndo = new ToolStripDropDownButton();
        ToolStripDropDownButton lstRedo = new ToolStripDropDownButton();

        #endregion	// Local Variables

        #region	Constructors
        public WordPad()
        {
            InitializeComponent();

            CommandManager.GetInstance().Attach(this);

            #region File Name
            filename = "Document";
            UpdateTitle();
            #endregion
        }

        private void InitializeComponent()
        {
            if (Configuration.ENVIRONMENT.CompareTo("Window") == 0)
            {
                guiFactory = new WindowGuiFactory();
            }
            else if (Configuration.ENVIRONMENT.CompareTo("Linux") == 0)
            {
                guiFactory = new LinuxGuiFactory();
            }
            else if (Configuration.ENVIRONMENT.CompareTo("Mac") == 0)
            {
                guiFactory = new MacGuiFactory();
            }
            #region Tool Bar
            toolbarpanel = guiFactory.MakePanel();
            toolbarpanel.SetHandler(this, "This is panel, which contain tool bar and formatting bar!");
            toolbarpanel.Click += new EventHandler(toolbarpanel_Click);
            this.Controls.Add(toolbarpanel);
            toolbarpanel.Dock = DockStyle.Top;

            #region Formatting Bar
            formattingBar = guiFactory.MakeToolStrip();
            formattingBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { cmdBold, cmdItalic, cmdUnderline, toolStripSeparator1, lstColors, lstFonts, lstFontSize, toolStripSeparator2, lstZoom });
            // 
            // cmdBold
            // 
            cmdBold.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            cmdBold.ImageTransparentColor = System.Drawing.Color.Magenta;
            cmdBold.Name = "cmdBold";
            cmdBold.Size = new System.Drawing.Size(31, 22);
            cmdBold.Text = "Bold";
            //cmdBold.Click += new System.EventHandler(this.cmdBold_Click);
            // 
            // cmdItalic
            // 
            cmdItalic.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            cmdItalic.ImageTransparentColor = System.Drawing.Color.Magenta;
            cmdItalic.Name = "cmdItalic";
            cmdItalic.Size = new System.Drawing.Size(34, 22);
            cmdItalic.Text = "Italic";
            //cmdItalic.Click += new System.EventHandler(this.cmdItalic_Click);
            // 
            // cmdUnderline
            // 
            cmdUnderline.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            cmdUnderline.ImageTransparentColor = System.Drawing.Color.Magenta;
            cmdUnderline.Name = "cmdUnderline";
            cmdUnderline.Size = new System.Drawing.Size(56, 22);
            cmdUnderline.Text = "Underline";
            //cmdUnderline.Click += new System.EventHandler(this.cmdUnderline_Click);
            // 
            // toolStripSeparator1
            // 
            toolStripSeparator1.Name = "toolStripSeparator1";
            toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // lstColors
            // 
            lstColors.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            lstColors.ImageTransparentColor = System.Drawing.Color.Magenta;
            lstColors.Name = "lstColors";
            lstColors.Size = new System.Drawing.Size(45, 22);
            lstColors.Text = "Color";
            //lstColors.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstColors_DropDownItemClicked);
            // 
            // lstFonts
            // 
            lstFonts.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            lstFonts.ImageTransparentColor = System.Drawing.Color.Magenta;
            lstFonts.Name = "lstFonts";
            lstFonts.Size = new System.Drawing.Size(42, 22);
            lstFonts.Text = "Font";
            //lstFonts.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstFonts_DropDownItemClicked);
            // 
            // lstFontSize
            // 
            lstFontSize.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            lstFontSize.ImageTransparentColor = System.Drawing.Color.Magenta;
            lstFontSize.Name = "lstFontSize";
            lstFontSize.Size = new System.Drawing.Size(39, 22);
            lstFontSize.Text = "Size";
            //lstFontSize.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstFontSize_DropDownItemClicked);
            // 
            // toolStripSeparator2
            // 
            toolStripSeparator2.Name = "toolStripSeparator2";
            toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // lstZoom
            // 
            lstZoom.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            lstZoom.ImageTransparentColor = System.Drawing.Color.Magenta;
            lstZoom.Name = "lstZoom";
            lstZoom.Size = new System.Drawing.Size(46, 22);
            lstZoom.Text = "Zoom";
            //lstZoom.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstZoom_DropDownItemClicked);
            // 
            // cmdAddImage
            // 
            cmdAddImage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            cmdAddImage.Location = new System.Drawing.Point(12, 237);
            cmdAddImage.Name = "cmdAddImage";
            cmdAddImage.Size = new System.Drawing.Size(154, 23);
            cmdAddImage.TabIndex = 2;
            cmdAddImage.Text = "Insert Image";
            cmdAddImage.UseVisualStyleBackColor = true;
            //cmdAddImage.Click += new System.EventHandler(this.cmdAddImage_Click);

            formattingBar.Location = new System.Drawing.Point(0, 0);
            formattingBar.Name = "formattingBar";
            formattingBar.Size = new System.Drawing.Size(341, 25);
            formattingBar.TabIndex = 1;
            formattingBar.Text = "formattingBar";
            formattingBar.SetHandler(toolbarpanel, "This is formatting bar, you can choose you text in text box, and select command which you want to format that text!");
            formattingBar.Click += new EventHandler(formattingBar_Click);
            toolbarpanel.Controls.Add(formattingBar);
            FormattingBarLoad();
            #endregion

            #region ActionBar
            actionBar = guiFactory.MakeToolStrip();
            actionBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { lstUndo, lstRedo});
            // 
            // lstUndo
            // 
            lstUndo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            lstUndo.ImageTransparentColor = System.Drawing.Color.Magenta;
            lstUndo.Name = "lstUndo";
            lstUndo.Size = new System.Drawing.Size(45, 22);
            lstUndo.Text = "Undo";
            //lstUndo.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstUndo_DropDownItemClicked);
            // 
            // lstRedo
            // 
            lstRedo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            lstRedo.ImageTransparentColor = System.Drawing.Color.Magenta;
            lstRedo.Name = "lstRedo";
            lstRedo.Size = new System.Drawing.Size(42, 22);
            lstRedo.Text = "Redo";
            //lstRedo.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.lstRedo_DropDownItemClicked);

            actionBar.Location = new System.Drawing.Point(0, 0);
            actionBar.Name = "actionBar";
            actionBar.Size = new System.Drawing.Size(341, 25);
            actionBar.TabIndex = 1;
            actionBar.Text = "actionBar";
            actionBar.SetHandler(toolbarpanel, "This is action bar, you can undo or redo you action!");
            actionBar.Click += new EventHandler(actionBar_Click);
            toolbarpanel.Controls.Add(actionBar);
            ActionBarLoad();
            #endregion

            toolbarpanel.Height = formattingBar.Height + actionBar.Height + 8;

            #endregion

            #region Status
            status = guiFactory.MakeStatusBar();
            status.Dock = DockStyle.Bottom;
            status.SetHandler(this, "This is status bar, which show text editor status!");
            status.Click += new EventHandler(status_Click);
            this.Controls.Add(status);
            #endregion

            #region Rich Text Edit
            edit = guiFactory.MakeRichTextBox();
            edit.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            edit.Dock = System.Windows.Forms.DockStyle.None;
            edit.HiglightColor = RtfColor.White;
            edit.Location = new System.Drawing.Point(0, toolbarpanel.Height);
            edit.Name = "rtBox_Main";
            edit.TabIndex = 0;
            edit.Text = "";
            edit.TextColor = RtfColor.Black;
            edit.SetHandler(this, "This is editor, you can edit your text here!");
            edit.Click += new EventHandler(edit_Click);
            RichTextBoxWithBorderAndScroll decoratedEditor = new RichTextBoxWithBorderAndScroll(edit);
            this.Controls.Add(decoratedEditor.GetRichTextBox());
            #endregion

            #region Menu
            menu = guiFactory.MakeWordPadMenu(this, status);
            this.Menu = menu.MainMenu;
            #endregion

            #region Event
            // Register all required events
            edit.TextChanged += new EventHandler(DocumentChanged);
            edit.SelectionChanged += new EventHandler(edit_SelectionChanged);
            cmdBold.Click += new EventHandler(cmdBold_Click);
            cmdItalic.Click += new EventHandler(cmdItalic_Click);
            cmdUnderline.Click += new EventHandler(cmdUnderline_Click);
            lstColors.DropDownItemClicked += new ToolStripItemClickedEventHandler(lstColors_DropDownItemClicked);
            lstFonts.DropDownItemClicked += new ToolStripItemClickedEventHandler(lstFonts_DropDownItemClicked);
            lstZoom.DropDownItemClicked += new ToolStripItemClickedEventHandler(lstZoom_DropDownItemClicked);
            lstFontSize.DropDownItemClicked += new ToolStripItemClickedEventHandler(lstFontSize_DropDownItemClicked);
            lstUndo.Click += new EventHandler(lstUndo_Click);
            lstRedo.Click += new EventHandler(lstRedo_Click);
            #endregion

            #region This Form
            this.SizeChanged += new EventHandler(WordPad_SizeChanged);
            this.HelpButtonClicked += new CancelEventHandler(WordPad_HelpButtonClicked);
            this.Click += new EventHandler(WordPad_Click);
            this.SetHandler(null, "This is text editor main form!");
            this.HelpButton = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            #endregion
        }
        #endregion	// Constructors

        #region Static Methods
        [STAThreadAttribute]
        public static int Main(string[] args)
        {
            wordpad = new WordPad();
            Application.Run(wordpad);

            return 0;
        }
        #endregion	// Static Methods

        #region Event Handlers
        private void DocumentChanged(object sender, EventArgs e)
        {
            wordpad.Changed = true;
        }

        void WordPad_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            IsHelp = true;
        }

        void WordPad_Click(object sender, EventArgs e)
        {
            if (IsHelp)
            {
                this.HandleHelp();
                IsHelp = false;
            }
        }

        void toolbarpanel_Click(object sender, EventArgs e)
        {
            if (IsHelp)
            {
                toolbarpanel.HandleHelp();
                IsHelp = false;
            }
        }

        void status_Click(object sender, EventArgs e)
        {
            if (IsHelp)
            {
                status.HandleHelp();
                IsHelp = false;
            }
        }

        void edit_Click(object sender, EventArgs e)
        {
            if (IsHelp)
            {
                edit.HandleHelp();
                IsHelp = false;
            }
        }

        void formattingBar_Click(object sender, EventArgs e)
        {
            if (IsHelp)
            {
                formattingBar.HandleHelp();
                IsHelp = false;
            }
        }

        void actionBar_Click(object sender, EventArgs e)
        {
            if (IsHelp)
            {
                actionBar.HandleHelp();
                IsHelp = false;
            }
        }

        void WordPad_SizeChanged(object sender, EventArgs e)
        {
            edit.Size = new Size(this.Width - 50, this.Height - status.Height - toolbarpanel.Height - 70);
        }

        void edit_SelectionChanged(object sender, EventArgs e)
        {
            if (edit.SelectionFont != null)
            {
                cmdBold.Checked = edit.SelectionFont.Bold;
                cmdItalic.Checked = edit.SelectionFont.Italic;
                cmdUnderline.Checked = edit.SelectionFont.Underline;
            }
        }

        void cmdBold_Click(object sender, EventArgs e)
        {
            if (edit.SelectionFont == null)
            {
                return;
            }

            FontStyle style = edit.SelectionFont.Style;

            if (edit.SelectionFont.Bold)
            {
                style &= ~FontStyle.Bold;
            }
            else
            {
                style |= FontStyle.Bold;
            }
            //edit.SelectionFont = new Font(edit.SelectionFont, style);
            Font font = new Font(edit.SelectionFont, style);
            CommandManager.GetInstance().NewChangeFontCommand(ref edit, font);
        }

        void cmdItalic_Click(object sender, EventArgs e)
        {
            if (edit.SelectionFont == null)
            {
                return;
            }
            FontStyle style = edit.SelectionFont.Style;

            if (edit.SelectionFont.Italic)
            {
                style &= ~FontStyle.Italic;
            }
            else
            {
                style |= FontStyle.Italic;
            }
            //edit.SelectionFont = new Font(edit.SelectionFont, style);
            Font font = new Font(edit.SelectionFont, style);
            CommandManager.GetInstance().NewChangeFontCommand(ref edit, font);
        }

        void cmdUnderline_Click(object sender, EventArgs e)
        {
            if (edit.SelectionFont == null)
            {
                return;
            }

            FontStyle style = edit.SelectionFont.Style;

            if (edit.SelectionFont.Underline)
            {
                style &= ~FontStyle.Underline;
            }
            else
            {
                style |= FontStyle.Underline;
            }
            //edit.SelectionFont = new Font(edit.SelectionFont, style);
            Font font = new Font(edit.SelectionFont, style);
            CommandManager.GetInstance().NewChangeFontCommand(ref edit, font);
        }

        void lstColors_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            KnownColor selectedColor;
            selectedColor = (KnownColor)System.Enum.Parse(typeof(KnownColor), e.ClickedItem.Text);

            //edit.SelectionColor = Color.FromKnownColor(selectedColor);
            Color color = Color.FromKnownColor(selectedColor);
            CommandManager.GetInstance().NewChangeColorCommand(ref edit, color);
        }

        void lstFonts_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (edit.SelectionFont == null)
            {
                edit.SelectionFont = new Font(e.ClickedItem.Text, edit.Font.Size);
            }
            //edit.SelectionFont = new Font(e.ClickedItem.Text, edit.SelectionFont.Size);
            Font font = new Font(e.ClickedItem.Text, edit.SelectionFont.Size);
            CommandManager.GetInstance().NewChangeFontCommand(ref edit, font);
        }

        void lstZoom_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            float zoomPercent = Convert.ToSingle(e.ClickedItem.Text.Trim('%'));
            //edit.ZoomFactor = zoomPercent / 100;
            float zoom = zoomPercent / 100;
            CommandManager.GetInstance().NewChangeZoomCommand(ref edit, zoom);
        }

        void lstFontSize_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (edit.SelectionFont == null)
            {
                return;
            }
            //edit.SelectionFont = new Font(edit.SelectionFont.FontFamily, Convert.ToInt32(e.ClickedItem.Text), edit.SelectionFont.Style); new Font(e.ClickedItem.Text, edit.SelectionFont.Size);
            Font font = new Font(edit.SelectionFont.FontFamily, Convert.ToInt32(e.ClickedItem.Text), edit.SelectionFont.Style); new Font(e.ClickedItem.Text, edit.SelectionFont.Size);
            CommandManager.GetInstance().NewChangeFontCommand(ref edit, font);
        }

        void lstUndo_Click(object sender, EventArgs e)
        {
            CommandManager.GetInstance().Undo();
        }

        void lstRedo_Click(object sender, EventArgs e)
        {
            CommandManager.GetInstance().Redo();
        }
        #endregion	// Event Handlers

        #region Properties
        public bool Changed
        {
            get
            {
                return changed;
            }

            set
            {
                if (changed != value)
                {
                    changed = value;
                    UpdateTitle();
                }
            }
        }

        public string Filename
        {
            get
            {
                return filename;
            }

            set
            {
                if (filename != value)
                {
                    filename = value;
                    UpdateTitle();
                }
            }
        }
        #endregion	// Properties

        #region Methods
        internal void InsertDateTime()
        {
            CommandManager.GetInstance().NewInsertTextCommand(ref edit, DateTime.Now.ToString());
        }

        internal void InsertRTF()
        {
            PlainTextDialog _dialog = new PlainTextDialog();

            if (DialogResult.OK == _dialog.ShowDialog())
            {
                CommandManager.GetInstance().NewInsertRTFCommand(ref edit, _dialog.PlainText, new Font(wordpad.Font, FontStyle.Bold | FontStyle.Underline), RtfColor.Blue, RtfColor.Yellow);
            }
        }

        internal void InsertPicture()
        {
            OpenFileDialog _dialog = new OpenFileDialog();
            _dialog.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|" +
                             "Windows Bitmap(*.bmp)|*.bmp|" +
                             "Windows Icon(*.ico)|*.ico|" +
                             "Graphics Interchange Format (*.gif)|(*.gif)|" +
                             "JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|" +
                             "Portable Network Graphics (*.png)|*.png|" +
                             "Tag Image File Format (*.tif)|*.tif;*.tiff";
            if (DialogResult.OK == _dialog.ShowDialog(wordpad))
            {
                try
                {
                    // If file is an icon
                    if (_dialog.FileName.ToUpper().EndsWith(".ICO"))
                    {
                        // Create a new icon, get it's handle, and create a bitmap from
                        // its handle
                        Image image = Bitmap.FromHicon((new Icon(_dialog.FileName)).Handle);
                        CommandManager.GetInstance().NewInsertPictureCommand(ref edit, image);
                    }
                    else
                    {
                        // Create a bitmap from the filename
                        Image image = Image.FromFile(_dialog.FileName);
                        CommandManager.GetInstance().NewInsertPictureCommand(ref edit, image);
                    }
                }
                catch (Exception _e)
                {
                    MessageBox.Show("The file could not be opened:\n\n" + _e.Message, "File Open Error");
                }
            }
        }

        public void Clear()
        {
            DialogResult result;

            if (Changed)
            {
                result = MessageBox.Show("Save changed to " + filename + "?", "WordPad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                if (result == DialogResult.Yes)
                {
                    Save(false);
                }
            }

            // FIXME - we need a dialog and ask for the document type
            filename = "Document";
            filetype = RichTextBoxStreamType.RichText;
            edit.Clear();
            Changed = false;
            UpdateTitle();
        }

        public void Exit()
        {
            DialogResult result;

            if (Changed)
            {
                result = MessageBox.Show("Save changed to " + filename + "?", "WordPad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                if (result == DialogResult.Yes)
                {
                    Save(false);
                }
            }
            Application.Exit();
        }

        public bool Open()
        {
            OpenFileDialog openfile;

            openfile = new OpenFileDialog();
            openfile.AddExtension = true;
            openfile.CheckFileExists = true;
            openfile.DereferenceLinks = true;
            openfile.DefaultExt = "rtf";
            openfile.Filter = "Rich Text Format (*.rtf)|*.rtf|Text files (*.txt)|*.txt|All files (*.*)|*.*";

            if (openfile.ShowDialog() == DialogResult.Cancel)
            {
                return false;
            }

            if (openfile.FilterIndex == 1)
            {
                return OpenFile(openfile.FileName, RichTextBoxStreamType.RichText);
            }
            else
            {
                return OpenFile(openfile.FileName, RichTextBoxStreamType.PlainText);
            }
        }

        public bool OpenFile(string name, RichTextBoxStreamType type)
        {
            if (Changed)
            {
                DialogResult result;

                result = MessageBox.Show("Save changed to " + filename + "?", "WordPad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Cancel)
                {
                    return false;
                }

                if (result == DialogResult.Yes)
                {
                    Save(false);
                }
            }

            filename = name;
            filetype = type;
            edit.LoadFile(filename, filetype);
            Changed = false;
            UpdateTitle();
            return true;
        }

        public bool Save(bool ask)
        {
            if ((filename == "Document") || ask)
            {
                SaveFileDialog savefile;
                DialogResult result;

                savefile = new SaveFileDialog();
                savefile.AddExtension = true;
                savefile.CheckFileExists = false;
                savefile.DereferenceLinks = true;
                savefile.DefaultExt = "rtf";
                savefile.Filter = "Rich Text Format (*.rtf)|*.rtf|Text files (*.txt)|*.txt|All files (*.*)|*.*";

                result = savefile.ShowDialog();

                if (result != DialogResult.OK)
                {
                    return false;
                }

                if (savefile.FilterIndex == 1)
                {
                    filetype = RichTextBoxStreamType.RichText;
                }
                else
                {
                    filetype = RichTextBoxStreamType.PlainText;
                }
                filename = savefile.FileName;
            }

            if (filename == String.Empty)
            {
                return false;
            }

            edit.SaveFile(filename, filetype);
            Changed = false;
            UpdateTitle();
            return true;
        }

        public void UpdateTitle()
        {
            string title;

            title = filename + (Changed ? "*" : "") + " - WordPad";
            this.Text = title;
        }

        public void DoCut()
        {
            CommandManager.GetInstance().NewCutCommand(ref edit);
        }

        internal void DoCopy()
        {
            edit.Copy();
        }

        internal void DoPaste()
        {
            CommandManager.GetInstance().NewPasteCommand(ref edit);
        }

        internal void DoClear()
        {
            CommandManager.GetInstance().NewClearCommand(ref edit);
        }

        internal void DoSelectAll()
        {
            CommandManager.GetInstance().NewSelectAllCommand(ref edit);
        }

        internal void DoFind()
        {
        }

        internal void DoFindNext()
        {
        }

        internal void DoReplace()
        {
        }

        private void UpdateActionBar()
        {
            if (CommandManager.GetInstance().CanUndo())
                lstUndo.Enabled = true;
            else
                lstUndo.Enabled = false;


            if (CommandManager.GetInstance().CanRedo())
                lstRedo.Enabled = true;
            else
                lstRedo.Enabled = false;
        }

        private void ActionBarLoad()
        {
            UpdateActionBar();
        }

        private void FormattingBarLoad()
        {
            lstColors.DropDown.Items.Add("Red");
            lstColors.DropDown.Items.Add("Blue");

            InstalledFontCollection fonts = new InstalledFontCollection();
            foreach (FontFamily family in fonts.Families)
            {
                lstFonts.DropDown.Items.Add(family.Name);
            }

            lstZoom.DropDown.Items.Add("300%");
            lstZoom.DropDown.Items.Add("200%");
            lstZoom.DropDown.Items.Add("100%");

            lstFontSize.DropDown.Items.Add("8");
            lstFontSize.DropDown.Items.Add("10");
            lstFontSize.DropDown.Items.Add("12");
        }
        #endregion	// Methods

        #region Implement Interface
        protected HelpHandler _successor;
        protected string _topic;
        public virtual bool HasHelp()
        {
            if (_topic == "")
                return false;
            return true;
        }

        public virtual void SetHandler(HelpHandler successor, string topic)
        {
            _successor = successor;
            _topic = topic;
        }

        public virtual void HandleHelp()
        {
            if (HasHelp())
            {
                MessageBox.Show(_topic);
            }
            else
            {
                if (_successor != null)
                {
                    _successor.HandleHelp();
                }
            }
        }
        #endregion

        #region Observer Members

        void Observer.Update()
        {
            UpdateActionBar();
        }

        #endregion
    }
}